package main.cipher;

import javax.xml.bind.DatatypeConverter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;

public class MD5Utils {
    public static String getChecksum(byte[] data) {
        String checksum = null;

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(data);
            byte[] digest = md.digest();
            checksum = DatatypeConverter.printHexBinary(digest);
        }
        catch (Exception e) {
        }

        return checksum;
    }

    public static String getChecksum(String filename) {
        String checksum = null;

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(Files.readAllBytes(Paths.get(filename)));
            byte[] digest = md.digest();
            checksum = DatatypeConverter.printHexBinary(digest);
        }
        catch (Exception e) {
        }

        return checksum;
    }
}
